// Copyright (c) 2024 刻BITTER
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.


#pragma once

/**
 * @brief   用于实现轮询式事件处理的简单工具
 */


namespace ioxx {

    /**
     * @brief 等待，直到对象的上一次非阻塞操作完成
     *
     * @tparam T 含有`bool ready()` 成员函数的类型
     * @param target
     */
    template <typename T>
    void wait(const T& target) {
        while (!target.ready());
    }


    template <typename T, typename CallbackType = void (*)(T&)>
    void when_ready(T& target, CallbackType func) {
        if (target.ready()) {
            func(target);
        }
    }

}  // namespace ioxx
